﻿$(function () {
    $("#btnSubmit").live("click", btnSubmitClick);
    $("#btnCancel").live("click", function () { location.reload();});
    $("#btnSubmitNewPwd").live("click", btnSubmitNewPwdClick);
    $("#btnCancelNewPwd").live("click", function () { $("#formModifyPwd :password").val(""); });
    
    $(".liItem").click(function () {
        var $this = $(this);
        var index = $this.index();
        $this.siblings().removeClass("liSelected");
        $this.addClass("liSelected");
        $(".IcenterItem").hide().eq(index).show();
    });

    function modifyPwdformCheck()
    {
        var res = true;
        var OldPwd = $("#OldPwd").val();
        var NewPwd = $("#NewPwd").val();
        var NewPwdCheck = $("#NewPwdCheck").val();

        if ($.trim(OldPwd) == "") {
            alert("请输入原密码");
            $("#OldPwd").focus();
            res = false;
            return res;
        }

        if ($.trim(NewPwd) == "" || $.trim(NewPwdCheck) == "" || NewPwd != NewPwdCheck) {
            alert("两次密码输入不一致");
            $("#NewPwd").focus();
            res = false;
            return res;
        }

        if (!(NewPwd.length >= 6 && NewPwd.length <= 12)) {
            alert("请输入长度为6到12位的密码");
            $("#NewPwd").focus();
            res = false;
            return res;
        }
        return res;
    }
    function btnSubmitNewPwdClick()
    {
        if (!modifyPwdformCheck()) {
            return;
        }
        var data = $("#formModifyPwd").serialize();
        $.ajax({
            url: "../I/ModifyPwd",
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
            },
            type: "post",
            success: function (result) {
                if (result.Result.IsSuccess) {
                    alert("修改成功");
                    $("#formModifyPwd :password").val("");
                    //location.reload();
                }
                else {
                    alert(result.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }
    function btnSubmitClick() {
        if (!formCheck()) {
            return;
        }
        var data = $("#formUerInfo").serialize();
        $.ajax({
            url: "../User/ModifyUserInfo",
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
            },
            type: "post",
            success: function (result) {
                if (result.Result.IsSuccess) {
                    alert("修改成功");
                    location.reload();
                }
                else {
                    alert(result.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }

    function formCheck() {
        var emialExp = /\w@\w*\.\w/;
        var mobileExp = /^(1[0-9]{10})$/;
        var res = true;
        var Mobile = $("#Mobile").val();
        if (!mobileExp.test(Mobile)) {
            alert("请输入正确格式的手机号码");
            $("#Mobile").focus();
            res = false;
            return res;
        }
        
        var Email = $("#Email").val();
        if (!emialExp.test(Email)) {
            alert("请输入正确格式的邮箱");
            $("#Email").focus();
            res = false;
            return res;
        }

        var Age = $("#Age").val();
        if (positivenumberCheck(Age)) {
            alert("请输入正确格式的年龄");
            $("#Age").focus();
            res = false;
            return res;
        }

        var Height = $("#Height").val();
        if (positivenumberCheck(Height)) {
            alert("请输入正确格式的身高");
            $("#Height").focus();
            res = false;
            return res;
        }


        var Weight = $("#Weight").val();
        if (positivenumberCheck(Weight)) {
            alert("请输入正确格式的体重");
            $("#Weight").focus();
            res = false;
            return res;
        }

        var EyeGlass = $("#EyeGlass").val();
        if (positivenumberCheck(EyeGlass)) {
            alert("请输入正确格式的眼镜度数");
            $("#EyeGlass").focus();
            res = false;
            return res;
        }
        var EyeLeft = $("#EyeLeft").val();
        if (positivenumberCheck(EyeLeft)) {
            alert("请输入正确格式的左眼视力");
            $("#EyeLeft").focus();
            res = false;
            return res;
        }

        var EyeRight = $("#EyeRight").val();
        if (positivenumberCheck(EyeRight)) {
            alert("请输入正确格式的右眼视力");
            $("#EyeRight").focus();
            res = false;
            return res;
        }
        return res;
    }

});